package cn.yunhe.controller;

import cn.yunhe.entity.News;
import cn.yunhe.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {

    //RequestBody：用于前台向后台传递复杂数据

    @RequestMapping("requestBody")
    public ModelAndView requestBody(@RequestBody User user) {
        ModelAndView mv = new ModelAndView();
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("result", 1);
        return mv;
    }

    @RequestMapping("requestBody_two")
    public ModelAndView requestBody_two(@RequestBody User user, @RequestHeader(value = "User-Agent", defaultValue = "foo") String userAgent) {
        System.out.println(user.getUsername());
        System.out.println(user.getNews().getContent());
        System.out.println("当前使用的浏览器版本信息为:" + userAgent);
        News news = new News("中兴", "歇逼");
        News news1 = new News("华为", "牛逼");
        News news2 = new News("联想", "凉凉");

        List<News> list = new ArrayList<>();
        list.add(news);
        list.add(news1);
        list.add(news2);

        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("newsList", list);
        return mv;
    }


}
